<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Requests\SysNoticeRequest;
use App\Models\SysNotice;
use Illuminate\Http\Request;

class SysNoticeController extends ApiBaseController
{
    public function indexNotice(){
        $items = SysNotice::where('status',1)->get();
        return success([
            'rows' => $items
        ]);
    }
    public function index(Request $request){
        $paginator = SysNotice::filter($request->all())->orderBy('id', 'desc')->paginate($this->pageSize);
        return success(page_format($paginator));

    }

    public function show(Request $request, SysNotice $model){
        $id = $request->get('id');
        $data = $model->find($id);
        return success(['data' => $data ?? []]);
    }

    public function create(SysNoticeRequest $request){
        $params = $request->all();
        $model = SysNotice::create($request->all());
        return success([],'添加成功');
    }

    public function update(SysNoticeRequest $request, SysNotice $model)
    {
        $id = $request->post('id');
        $dept = $model->find($id);
        $validated = $request->validated();

        if($dept){
            $dept->update($request->all());
        }else{
            return error('数据不存在');
        }

        return success([],'更新成功');
    }

    public function destroy($ids, Request $request, SysNotice $model){
        $id_arr = explode(',', $ids);
        $model->destroy($id_arr);
        return success([],'删除成功');
    }

}
